package com.sk.ccflower.server.lib.dbUtils.qry;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import com.sk.ccflower.lib.commonBeans.DefaultParamBean;
import com.sk.ccflower.server.lib.dbBeans.MfCommentBean;
import com.sk.ccflower.server.lib.dbUtils.DBConnection;
import com.sk.ccflower.server.lib.dbUtils.SQLReturn;
import com.sk.ccflower.server.lib.dbUtils.SQLRunner;
import com.sk.ccflower.server.lib.dbUtils.sql.MfCommentSql;

public class MfCommentQry extends MfCommentSql {
	
	protected DBConnection m_db = null;
	protected SQLRunner m_sqlRun = null;
	protected Connection m_conn = null;
	
	public MfCommentQry() throws Exception {
		super();
	}
	
	public MfCommentQry(DBConnection _db) throws Exception {
        m_db = _db;
        m_sqlRun = new SQLRunner();
        m_conn = m_db.getConnection();
    }
	
	public MfCommentQry(Connection _conn) throws Exception {
        m_sqlRun = new SQLRunner();
        m_conn = _conn;
    }
	
	private int executeQuery(SQLReturn arg) throws SQLException, Exception {
        ArrayList<Object> arr = null;
        
        m_sqlRun.begin(m_conn, false);
        arr = m_sqlRun.executeQuery(arg.m_query, arg.m_params, arg.m_class);
        m_sqlRun.end();
        
        int n = put(arr);
        
        m_log.debug("size = " + n);

        return n;
    }
	
	public int select() throws SQLException, Exception {
		return executeQuery( getSql() );
	}
	
	public int select( String email ) throws SQLException, Exception {
		return executeQuery( getSql(email) );
	}
	
	public int select( String src_user, String dest_user ) throws SQLException, Exception {
		return executeQuery( getSql(src_user, dest_user) );
	}
	
	public void insert( MfCommentBean bean ) throws SQLException, Exception {
		try {
			SQLReturn arg = getSqlInsert(bean);

			m_sqlRun.begin(m_conn, false);
			m_sqlRun.executeInsert(arg.m_query, bean, arg.m_colMap);
            
            m_sqlRun.commit();
        } catch (SQLException e) {
            m_sqlRun.rollback();
            m_log.error(e.toString(), e);
            throw e;
        } catch (Exception e) {
            m_sqlRun.rollback();
            m_log.error(e.toString(), e);
            throw e;
        } finally {
            m_sqlRun.end();
        }
	}
	
	public void update(MfCommentBean bean) throws SQLException, Exception {
        try {
            m_sqlRun.begin(m_conn, false);
            
            SQLReturn arg = getSqlUpdate(bean);
            m_sqlRun.executeUpdate( arg.m_query, arg.m_params );
            
            m_sqlRun.commit();
        } catch (SQLException e) {
            m_sqlRun.rollback();
            m_log.error(e.toString() , e);
            throw e;
        } catch (Exception e) {
            m_sqlRun.rollback();
            m_log.error(e.toString() , e);
            throw e;
        } finally {
            m_sqlRun.end();
        }
    }
	
	public void delete( String src_user, String dest_user ) throws SQLException, Exception {
		try {
            m_sqlRun.begin(m_conn, false);
            
            SQLReturn arg = getSqlDelete(src_user, dest_user);
    		m_sqlRun.executeDelete( arg.m_query, arg.m_params );
            
            m_sqlRun.commit();
        } catch (SQLException e) {
            m_sqlRun.rollback();
            m_log.error(e.toString() , e);
            throw e;
        } catch (Exception e) {
            m_sqlRun.rollback();
            m_log.error(e.toString() , e);
            throw e;
        } finally {
            m_sqlRun.end();
        }
	}
	
	public void scoreInit( DefaultParamBean bean ) throws SQLException, Exception {
		try {
			m_sqlRun.begin(m_conn, false);
            SQLReturn arg = getSqlscoreInit(bean);
            m_sqlRun.executeUpdate( arg.m_query, arg.m_params );
    		m_sqlRun.commit();
        } catch (SQLException e) {
            m_sqlRun.rollback();
            m_log.error(e.toString() , e);
            throw e;
        } catch (Exception e) {
            m_sqlRun.rollback();
            m_log.error(e.toString() , e);
            throw e;
        } finally {
            m_sqlRun.end();
        }
	}
	
}
